package com.mingtu.jgspatrol.activity;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.mingtu.common.activity.LoginActivity;
import com.mingtu.common.activity.WebActivity;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.SPTools;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.databinding.ActivityAgreementBinding;

public class AgreementActivity extends MyBaseActivity<ActivityAgreementBinding> {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agreement;
    }

    @Override
    protected void initView() {
        hideHeadView();

    }

    @Override
    protected void initData() {
        String appName = AppUtils.getAppName();
        if (!StringUtils.isEmpty(appName)) {
            binding.tvAppName.setText(appName);
        }

        String content = "欢迎使用" + appName + "！在你使用前，请仔细阅读并了解用户协议和隐私政策，以了解我们的服务内容和我们在收集和使用你相关个人信息时的处理规则。我们将严格按照《用户协议》和《隐私政策》为你提供服务，保护你的个人信息。";

        String textClickable1 = "《用户协议》";
        String textClickable2 = "《隐私政策》";
        int start1 = content.indexOf(textClickable1);
        int start2 = content.indexOf(textClickable2);

        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("title", "用户协议");
                bundle.putString("url", Constant.getInstance().getURL_AGREEMENT());
                IntentUtils.getInstance().readyGo(AgreementActivity.this, WebActivity.class, bundle);
            }
        }, start1, start1 + textClickable1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("title", "隐私政策");
                bundle.putString("url", Constant.getInstance().getURL_PRIVACY());
                IntentUtils.getInstance().readyGo(AgreementActivity.this, WebActivity.class, bundle);
            }
        }, start2, start2 + textClickable2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorPrimary)), start1, start1 + textClickable1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorPrimary)), start2, start2 + textClickable2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.tvContent.setText(spannableString);
        binding.tvContent.setMovementMethod(LinkMovementMethod.getInstance());//设置可点击状态

        addClickListener(this,binding.butAgree,binding.tvNoAgree);
    }

    @Override
    protected void getData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.but_agree:
                agreeEvent();
                break;
            case R.id.tv_no_agree:
                new XPopup.Builder(this)
                        .isDestroyOnDismiss(true)
                        .dismissOnTouchOutside(true)
                        .hasShadowBg(true)
                        .asConfirm("协议提示", "您需要同意相关协议才能使用哦，不同意将无法使用我们的产品和服务。",
                                "放弃使用", "同意并继续",
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        agreeEvent();
                                    }
                                }, new OnCancelListener() {
                                    @Override
                                    public void onCancel() {
                                        SPStaticUtils.put(SPTools.isAgree, false);
                                        AppUtils.exitApp();

                                    }
                                }, false)
                        .show();
                break;
        }
    }


    private void agreeEvent() {
        SPStaticUtils.put(SPTools.isAgree, true);
        String token = SPStaticUtils.getString(SPTools.token);
        if (!StringUtils.isEmpty(token)) {
            IntentUtils.getInstance().readyGoThenKill(AgreementActivity.this, MainActivity.class);
        } else {
            IntentUtils.getInstance().readyGoThenKill(AgreementActivity.this, LoginActivity.class);
        }
    }
}